package com.xh.login.controller;

import com.xh.login.entity.User;
import com.xh.login.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 张园园
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;


    @RequestMapping("/login")
    public ModelAndView login(User user) {
        System.out.println("user = " + user);
        ModelAndView mv = new ModelAndView("login");
        User user1 = userService.login(user);
        System.out.println(user1);
        if(user1 == null){
            mv.addObject("user", "登陆失败");
            return mv;
        }
        mv.addObject("user","登陆成功");
        return mv;
    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(User user) {
        try {
            userService.register(user);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "err";
        }
    }
}
